package com.example.restatr.Atcivity.YouthInn;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.YouthInn.Bean.PolicyBean;
import com.example.restatr.Atcivity.YouthInn.Bean.TalentPolicyAreaInfoBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class AreaInfoAt extends AppCompatActivity {
    private TalentPolicyAreaInfoBean.DataBean data;
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ImageView head;
    private TextView name;
    private TextView workStartAt;
    private RecyclerView rv;
    private LinearLayout Null;
    private TextView nullText;
    private LinearLayout loading;
    private List<PolicyBean.DataBean> policyRows = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talent_policy_at);
        data = new Gson().fromJson(getIntent().getStringExtra("data"), TalentPolicyAreaInfoBean.class).getData();
        bindView();
        Tools.SetBarTitle(toolbar, barTitle, "区域政策", search, this, false);
        Glide.with(this)
                .load(Tools.ip + data.getImgUrl())
                .transform(new RoundedCorners(15))
                .into(head);
        name.setText(data.getName());
        workStartAt.setText(data.getIntroduce());
        nullText.setText("暂无政策...");
        GetPolicyList();
    }

    private void GetPolicyList() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/youth-inn/talent-policy/list?areaId=" + data.getId(), MainActivity.sp.getString("token", ""));
            PolicyBean policyBean = new Gson().fromJson(token, PolicyBean.class);
            policyRows.addAll(policyBean.getData());
            runOnUiThread(() -> {
                loading.setVisibility(View.GONE);
                if (policyRows.size() > 0) {
                    rv.setAdapter(new adapter());
                } else {
                    Null.setVisibility(View.VISIBLE);
                }
            });
        }).start();
    }

    private void bindView() {
        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        head = findViewById(R.id.head);
        name = findViewById(R.id.name);
        workStartAt = findViewById(R.id.workStartAt);
        rv = findViewById(R.id.rv);
        Null = findViewById(R.id.Null);
        nullText = findViewById(R.id.null_text);
        loading = findViewById(R.id.loading);

    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.zfz_list_item, parent, false);
            return new viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.tupian.setVisibility(View.GONE);
            holder.name.setText(policyRows.get(position).getTitle());
            holder.diz.setText(policyRows.get(position).getAuthor());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GetPolicyInfo(policyRows.get(position).getId());
                }
            });
        }

        @Override
        public int getItemCount() {
            return policyRows.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private ImageView tupian;
            private TextView name;
            private TextView diz;

            public viewHolder(@NonNull View itemView) {
                super(itemView);
                tupian = itemView.findViewById(R.id.tupian);
                name = itemView.findViewById(R.id.name);
                diz = itemView.findViewById(R.id.diz);
            }
        }
    }

    private void GetPolicyInfo(int id) {
        ProgressDialog show = ProgressDialog.show(this, "", "加载中...");
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/youth-inn/talent-policy/" + id, MainActivity.sp.getString("token", ""));
            runOnUiThread(() -> {
                Intent intent = new Intent(AreaInfoAt.this, PolicyAt.class);
                intent.putExtra("data", token);
                show.dismiss();
                startActivity(intent);
            });
        }).start();
    }

}